package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.entity.SysRole;
import com.medical.lsq.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleCotroller {
    @Autowired
    private RoleService roleService;
    /**
     *分页获取行政区域列表
     *@param currentPage
     * @param limit
     * 	 @param request
     * 	  @param model
     * 	  @return
     */
    @RequestMapping("/getRoleList")
    public String getRoleList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String roleName = request.getParameter("context");
        currentPage = currentPage !=null ? currentPage : 1;
        limit = limit !=null ? limit : 10;
        PageInfo<SysRole>pageInfo = roleService.getRoleList(roleName, currentPage, limit);
        model.addAttribute("PageInfo",pageInfo);
        model.addAttribute("context",roleName);
        System.out.println("pageInfo;"+pageInfo);
        System.out.println("context"+roleName);
        return "/role/role_list";
    }
    @RequestMapping("/openAdd")
        public String openAdd( Model model) { return "/role/role_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id, Model model) {
        SysRole role = roleService.getRoleById(id);
        model.addAttribute("role",role);
        return "/role/role_edit";

    }

    @RequestMapping("add")
    public String add(SysRole role, HttpServletRequest request, Model model) {
        roleService.add(role);
        return getRoleList(1,10,request,model);
    }
    @RequestMapping("eidt")
   public String edit(SysRole role,HttpServletRequest request, Model model) {
        roleService.edit(role);
        return getRoleList(1,10,request,model);
    }
    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model )  {
        roleService.delete(id);
       return getRoleList(1,10, request, model);

    }
    @ResponseBody
    @RequestMapping("getRoleTreeList")
    public Object getRoleTreeList() {
        Map<String , Object> map = new HashMap<String, Object>();
        map.put("roleList",roleService.getRoleTree(""));
        return map;
    }


}
